package cn.mavor.day0929.SuperMarket;

import java.io.*;
import java.util.Scanner;

public class SupermarketManagement {

    private static final String DATA_FILE = "supermarket.dat";
    private static Supermarket supermarket;

    public static void main(String[] args) {
        loadSupermarket();

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择操作：");
            System.out.println("1. 开卡");
            System.out.println("2. 查询剩余积分");
            System.out.println("3. 积分累计");
            System.out.println("4. 积分兑换");
            System.out.println("5. 修改密码");
            System.out.println("6. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    openAccount(scanner);
                    break;
                case 2:
                    checkPoints(scanner);
                    break;
                case 3:
                    accumulatePoints(scanner);
                    break;
                case 4:
                    redeemPoints(scanner);
                    break;
                case 5:
                    changePassword(scanner);
                    break;
                case 6:
                    saveAndExit();
                    return;
                default:
                    System.out.println("无效的选择，请重试。");
            }
        }
    }

    private static void loadSupermarket() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DATA_FILE))) {
            supermarket = (Supermarket) ois.readObject();
            System.out.println("已从文件加载超市数据。");
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("未能找到现有数据，正在初始化新超市...");
            supermarket = new Supermarket("MySupermarket", "Address Here");
        }
    }

    private static void saveSupermarket() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DATA_FILE))) {
            oos.writeObject(supermarket);
            System.out.println("超市数据已保存到文件。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void openAccount(Scanner scanner) {
        System.out.print("请输入卡号: ");
        String cardNumber = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请设置密码: ");
        String password = scanner.nextLine();

        Member member = new Member(cardNumber, name, password);
        supermarket.addMember(member);
        System.out.println("开卡成功！");
    }

    private static void checkPoints(Scanner scanner) {
        System.out.print("请输入卡号: ");
        String cardNumber = scanner.nextLine();
        Member member = supermarket.findMemberByCardNumber(cardNumber);
        if (member != null) {
            System.out.println("您的积分是: " + member.getPoints());
        } else {
            System.out.println("未找到该卡号。");
        }
    }

    private static void accumulatePoints(Scanner scanner) {
        System.out.print("请输入卡号: ");
        String cardNumber = scanner.nextLine();
        System.out.print("请输入要增加的积分: ");
        int points = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        Member member = supermarket.findMemberByCardNumber(cardNumber);
        if (member != null) {
            member.addPoints(points);
            System.out.println("积分增加成功！");
        } else {
            System.out.println("未找到该卡号。");
        }
    }

    private static void redeemPoints(Scanner scanner) {
        System.out.print("请输入卡号: ");
        String cardNumber = scanner.nextLine();
        System.out.print("请输入要兑换的积分: ");
        int points = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        Member member = supermarket.findMemberByCardNumber(cardNumber);
        if (member != null && member.redeemPoints(points)) {
            System.out.println("积分兑换成功！");
        } else {
            System.out.println("积分不足或未找到该卡号。");
        }
    }

    private static void changePassword(Scanner scanner) {
        System.out.print("请输入卡号: ");
        String cardNumber = scanner.nextLine();
        System.out.print("请输入旧密码: ");
        String oldPassword = scanner.nextLine();
        System.out.print("请输入新密码: ");
        String newPassword = scanner.nextLine();

        Member member = supermarket.findMemberByCardNumber(cardNumber);
        if (member != null && member.getPassword().equals(oldPassword)) {
            member.setPassword(newPassword);
            System.out.println("密码修改成功！");
        } else {
            System.out.println("卡号或密码错误。");
        }
    }

    private static void saveAndExit() {
        saveSupermarket();
        System.out.println("感谢使用，再见！");
    }
}